package file;

import java.io.*;
import java.util.Scanner;

public class Demo4 {
    //进⾏普通⽂件的复制
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源文件路径: ");
        String srcPath = scanner.next();
        File srcFile = new File(srcPath);
        //判断源文件是否存在
        if (!srcFile.isFile()) {
            System.out.println("源文件不存在或路径不合理");
            return;
        }

        System.out.println("请输入目标文件的路径: ");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        //判断 目标文件的 父路径是否存在，目标文件可以不存在
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("目标文件所在的路径不存在");
            return;
        }

        //都正确就进行复制
        try (InputStream inputStream = new FileInputStream(srcFile);
             OutputStream outputStream = new FileOutputStream(destFile)) {

            while(true) {
                byte[] data = new byte[1024];
                int n = inputStream.read(data);
                if (n == -1) {
                    return;
                }
                //这里读出多少，写多少，就是n
                outputStream.write(data,0,n);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
